SlideShare a Scribd company logo
Class No.16  Data Structures http://ecomputernotes.com
Deleting a node in BST As is common with many data structures, the hardest operation is deletion. Once we have found the node to be deleted, we need to consider several possibilities. If the node is a  leaf , it can be deleted immediately. http://ecomputernotes.com
Deleting a node in BST If the node has one child, the node can be deleted after its parent adjusts a pointer to bypass the node and connect to inorder successor. 6 2 4 3 1 8 http://ecomputernotes.com
Deleting a node in BST The inorder traversal order has to be maintained after the delete. 6 2 4 3 1 8 6 2 4 3 1 8  http://ecomputernotes.com
Deleting a node in BST The inorder traversal order has to be maintained after the delete. 6 2 4 3 1 8 6 2 4 3 1 8 6 2 3 1 8   http://ecomputernotes.com
Deleting a node in BST The complicated case is when the node to be deleted has both left and right subtrees. The strategy is to replace the data of this node with the smallest data of the right subtree and recursively delete that node. http://ecomputernotes.com
Deleting a node in BST Delete(2): locate inorder successor 6 2 5 3 1 8 4 Inorder  successor http://ecomputernotes.com
Deleting a node in BST Delete(2): locate inorder successor 6 2 5 3 1 8 4 Inorder  successor Inorder successor will be the left-most node in the right subtree of 2. The inorder successor will not have a left child because if it did, that child would be the left-most node. http://ecomputernotes.com
Deleting a node in BST Delete(2): copy data from inorder successor 6 2 5 3 1 8 4  6 3 5 3 1 8 4 http://ecomputernotes.com
Deleting a node in BST Delete(2): remove the inorder successor 6 2 5 3 1 8 4  6 3 5 3 1 8 4  6 3 5 3 1 8 4 http://ecomputernotes.com
Deleting a node in BST Delete(2)  6 3 5 4 1 8  6 3 5 3 1 8 4 http://ecomputernotes.com
C++ code for delete ‘ delete’ is C++ keyword. We will call our deleteNode routine remove. Here is the C++ code for remove. http://ecomputernotes.com
C++ code for delete TreeNode<int>* remove(TreeNode<int>* tree, int info) { TreeNode<int>* t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }  http://ecomputernotes.com
C++ code for delete TreeNode<int>* remove(TreeNode<int>* tree, int info) { TreeNode<int>* t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }  http://ecomputernotes.com
C++ code for delete TreeNode<int>* remove(TreeNode<int>* tree, int info) { TreeNode<int>* t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }  http://ecomputernotes.com
C++ code for delete TreeNode<int>* remove(TreeNode<int>* tree, int info) { TreeNode<int>* t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }  http://ecomputernotes.com
C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL  && tree->getRight() != NULL ){  TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }  http://ecomputernotes.com
C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL  && tree->getRight() != NULL ){  TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }  http://ecomputernotes.com
C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL  && tree->getRight() != NULL ){  TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }  http://ecomputernotes.com
C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL  && tree->getRight() != NULL ){  TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }  http://ecomputernotes.com
C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL  && tree->getRight() != NULL ){  TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }  http://ecomputernotes.com
C++ code for delete else { //  case 1 TreeNode<int>* nodeToDelete = tree; if( tree->getLeft() == NULL )  // will handle 0 children tree = tree->getRight();  else if( tree->getRight() == NULL ) tree = tree->getLeft(); else tree = NULL;  delete nodeToDelete; } return tree; }  http://ecomputernotes.com
C++ code for delete else { //  case 1 TreeNode<int>* nodeToDelete = tree; if( tree->getLeft() == NULL )  // will handle 0 children tree = tree->getRight();  else if( tree->getRight() == NULL ) tree = tree->getLeft(); else tree = NULL;  delete nodeToDelete; } return tree; }  http://ecomputernotes.com
C++ code for delete else { //  case 1 TreeNode<int>* nodeToDelete = tree; if( tree->getLeft() == NULL )  // will handle 0 children tree = tree->getRight();  else if( tree->getRight() == NULL ) tree = tree->getLeft(); else tree = NULL;  delete nodeToDelete; } return tree; }  http://ecomputernotes.com
C++ code for delete TreeNode<int>* findMin(TreeNode<int>* tree) { if( tree == NULL )  return NULL; if( tree->getLeft() == NULL )  return tree; //  this is it. return findMin( tree->getLeft() ); }  http://ecomputernotes.com
C++ code for delete TreeNode<int>* findMin(TreeNode<int>* tree) { if( tree == NULL )  return NULL; if( tree->getLeft() == NULL )  return tree; //  this is it. return findMin( tree->getLeft() ); }  http://ecomputernotes.com
C++ code for delete TreeNode<int>* findMin(TreeNode<int>* tree) { if( tree == NULL )  return NULL; if( tree->getLeft() == NULL )  return tree; //  this is it. return findMin( tree->getLeft() ); }  http://ecomputernotes.com
BinarySearchTree.h Let us design the BinarySearchTree class (factory). http://ecomputernotes.com
BinarySearchTree.h #ifndef _BINARY_SEARCH_TREE_H_ #define _BINARY_SEARCH_TREE_H_ #include <iostream.h> // For NULL // Binary node and forward declaration template <class EType> class BinarySearchTree;  http://ecomputernotes.com
BinarySearchTree.h #ifndef _BINARY_SEARCH_TREE_H_ #define _BINARY_SEARCH_TREE_H_ #include <iostream.h> // For NULL // Binary node and forward declaration template <class EType> class BinarySearchTree;  http://ecomputernotes.com
BinarySearchTree.h template <class EType> class BinaryNode { EType element; BinaryNode *left; BinaryNode *right; BinaryNode( const EType & theElement,  BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ),  right( rt ) { } friend class BinarySearchTree<EType>; };  http://ecomputernotes.com
BinarySearchTree.h template <class EType> class BinaryNode { EType element; BinaryNode *left; BinaryNode *right; BinaryNode( const EType & theElement,  BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ),  right( rt ) { } friend class BinarySearchTree<EType>; };  http://ecomputernotes.com
BinarySearchTree.h template <class EType> class BinaryNode { EType element; BinaryNode *left; BinaryNode *right; BinaryNode( const EType & theElement,  BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ),  right( rt ) { } friend class BinarySearchTree<EType>; };  http://ecomputernotes.com
BinarySearchTree.h template <class EType> class BinaryNode { EType element; BinaryNode *left; BinaryNode *right; BinaryNode( const EType & theElement,  BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ),  right( rt ) { } friend class BinarySearchTree<EType>; };  http://ecomputernotes.com
BinarySearchTree.h template <class EType> class BinarySearchTree { public: BinarySearchTree( const EType& notFound ); BinarySearchTree( const BinarySearchTree& rhs ); ~BinarySearchTree( ); const EType& findMin( ) const; const EType& findMax( ) const; const EType& find( const EType & x ) const; bool isEmpty( ) const; void printInorder( ) const; http://ecomputernotes.com
BinarySearchTree.h void insert( const EType& x ); void remove( const EType& x ); const BinarySearchTree & operator= ( const BinarySearchTree & rhs ); http://ecomputernotes.com
BinarySearchTree.h private: BinaryNode<EType>* root; // ITEM_NOT_FOUND object used to signal failed finds const EType ITEM_NOT_FOUND; const EType& elementAt( BinaryNode<EType>* t ); void insert(const EType& x, BinaryNode<EType>* & t); void remove(const EType& x, BinaryNode<EType>* & t); BinaryNode<EType>* findMin(BinaryNode<EType>* t); BinaryNode<EType>* findMax(BinaryNode<EType>* t); BinaryNode<EType>* find(const EType& x, BinaryNode<EType>* t ); void makeEmpty(BinaryNode<EType>* & t); void printInorder(BinaryNode<EType>* t); }; #endif http://ecomputernotes.com

More Related Content

TXT
Ass2 1 (2)
PDF
Designing linear algebra into Julia
DOC
C - aptitude3
PDF
Visualization of Supervised Learning with {arules} + {arulesViz}
PDF
밑바닥부터 시작하는 의료 AI
PPTX
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
PDF
Association Rule Mining with R
Ass2 1 (2)
Designing linear algebra into Julia
C - aptitude3
Visualization of Supervised Learning with {arules} + {arulesViz}
밑바닥부터 시작하는 의료 AI
Programming Java - Lection 07 - Puzzlers - Lavrentyev Fedor
Association Rule Mining with R

What's hot (12)

PDF
CS323: Binary Search Trees
TXT
Game in ex
PDF
Numpy tutorial(final) 20160303
PDF
Regression and Classification with R
PDF
CS323: Sort - Distribution-based
PDF
CS323: Trie
PPTX
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
PDF
手把手教你 R 語言分析實務
PDF
Python Peculiarities
PDF
PDF
Java Basics - Part1
PDF
CS323: Balanced Binary Search Trees
CS323: Binary Search Trees
Game in ex
Numpy tutorial(final) 20160303
Regression and Classification with R
CS323: Sort - Distribution-based
CS323: Trie
Programming Java - Lection 04 - Generics and Lambdas - Lavrentyev Fedor
手把手教你 R 語言分析實務
Python Peculiarities
Java Basics - Part1
CS323: Balanced Binary Search Trees
Ad

Viewers also liked (20)

PPT
computer notes - Data Structures - 25
PPT
computer notes - Data Structures - 23
PPT
computer notes - Data Structures - 1
PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 6
PPT
computer notes - Data Structures - 21
PPT
computer notes - Data Structures - 36
PPT
computer notes - Data Structures - 37
PPT
computer notes - Data Structures - 32
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 13
PPT
computer notes - Data Structures - 3
PPT
computer notes - Data Structures - 10
PPT
computer notes - Data Structures - 15
PPT
computer notes - Data Structures - 29
PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 38
PPT
computer notes - Data Structures - 33
PPT
computer notes - Data Structures - 5
PPT
computer notes - Data Structures - 17
computer notes - Data Structures - 25
computer notes - Data Structures - 23
computer notes - Data Structures - 1
computer notes - Data Structures - 18
computer notes - Data Structures - 6
computer notes - Data Structures - 21
computer notes - Data Structures - 36
computer notes - Data Structures - 37
computer notes - Data Structures - 32
computer notes - Data Structures - 4
computer notes - Data Structures - 13
computer notes - Data Structures - 3
computer notes - Data Structures - 10
computer notes - Data Structures - 15
computer notes - Data Structures - 29
computer notes - Data Structures - 39
computer notes - Data Structures - 38
computer notes - Data Structures - 33
computer notes - Data Structures - 5
computer notes - Data Structures - 17
Ad

Similar to computer notes - Data Structures - 16 (20)

PPT
Data structures lecture 04
DOCX
For this project, write a program that stores integers in a binary.docx
PPT
Bsides
 
PPT
C++: inheritance, composition, polymorphism
PPT
Computer notes data structures - 9
PPTX
(Slightly) Smarter Smart Pointers
PPT
Python Objects
PDF
Please read the comment ins codeExpressionTree.java-------------.pdf
PDF
in this assignment you are asked to write a simple driver program an.pdf
PDF
To create a node for a binary search tree (BTNode, ) and to create a .pdf
DOCX
Write a C++ function to delete the given value from the binary search.docx
PPT
Computer notes - Sorting
PPT
Move Accumulation To Collecting Parameter
PDF
Stop Monkeys Fall
PPT
Computer notes - Binary Search Tree with Strings
PPT
Exception Handling1
PDF
create a binary search tree from an empty one by adding the key valu.pdf
PPTX
Category theory, Monads, and Duality in the world of (BIG) Data
PPT
Antlr V3
Data structures lecture 04
For this project, write a program that stores integers in a binary.docx
Bsides
 
C++: inheritance, composition, polymorphism
Computer notes data structures - 9
(Slightly) Smarter Smart Pointers
Python Objects
Please read the comment ins codeExpressionTree.java-------------.pdf
in this assignment you are asked to write a simple driver program an.pdf
To create a node for a binary search tree (BTNode, ) and to create a .pdf
Write a C++ function to delete the given value from the binary search.docx
Computer notes - Sorting
Move Accumulation To Collecting Parameter
Stop Monkeys Fall
Computer notes - Binary Search Tree with Strings
Exception Handling1
create a binary search tree from an empty one by adding the key valu.pdf
Category theory, Monads, and Duality in the world of (BIG) Data
Antlr V3

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 20
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 31
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 35
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 14
DOC
Computer notes - Controlling User Access
DOC
Computer notes - Using SET Operator
computer notes - Data Structures - 30
computer notes - Data Structures - 11
computer notes - Data Structures - 20
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 28
computer notes - Data Structures - 19
computer notes - Data Structures - 31
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 22
computer notes - Data Structures - 35
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 14
Computer notes - Controlling User Access
Computer notes - Using SET Operator

Recently uploaded (20)

PDF
Deliverable file - Regulatory guideline analysis.pdf
PDF
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
PPT
Chapter four Project-Preparation material
PDF
Stem Cell Market Report | Trends, Growth & Forecast 2025-2034
PPTX
Probability Distribution, binomial distribution, poisson distribution
PDF
A Brief Introduction About Julia Allison
PDF
COST SHEET- Tender and Quotation unit 2.pdf
PPT
Data mining for business intelligence ch04 sharda
PDF
Chapter 5_Foreign Exchange Market in .pdf
PDF
How to Get Funding for Your Trucking Business
PPTX
AI-assistance in Knowledge Collection and Curation supporting Safe and Sustai...
PDF
MSPs in 10 Words - Created by US MSP Network
PPTX
Belch_12e_PPT_Ch18_Accessible_university.pptx
PPTX
Dragon_Fruit_Cultivation_in Nepal ppt.pptx
PDF
Unit 1 Cost Accounting - Cost sheet
PDF
Types of control:Qualitative vs Quantitative
PPTX
The Marketing Journey - Tracey Phillips - Marketing Matters 7-2025.pptx
PDF
WRN_Investor_Presentation_August 2025.pdf
PDF
Power and position in leadershipDOC-20250808-WA0011..pdf
PDF
Katrina Stoneking: Shaking Up the Alcohol Beverage Industry
Deliverable file - Regulatory guideline analysis.pdf
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
Chapter four Project-Preparation material
Stem Cell Market Report | Trends, Growth & Forecast 2025-2034
Probability Distribution, binomial distribution, poisson distribution
A Brief Introduction About Julia Allison
COST SHEET- Tender and Quotation unit 2.pdf
Data mining for business intelligence ch04 sharda
Chapter 5_Foreign Exchange Market in .pdf
How to Get Funding for Your Trucking Business
AI-assistance in Knowledge Collection and Curation supporting Safe and Sustai...
MSPs in 10 Words - Created by US MSP Network
Belch_12e_PPT_Ch18_Accessible_university.pptx
Dragon_Fruit_Cultivation_in Nepal ppt.pptx
Unit 1 Cost Accounting - Cost sheet
Types of control:Qualitative vs Quantitative
The Marketing Journey - Tracey Phillips - Marketing Matters 7-2025.pptx
WRN_Investor_Presentation_August 2025.pdf
Power and position in leadershipDOC-20250808-WA0011..pdf
Katrina Stoneking: Shaking Up the Alcohol Beverage Industry

computer notes - Data Structures - 16

  • 1. Class No.16 Data Structures http://ecomputernotes.com
  • 2. Deleting a node in BST As is common with many data structures, the hardest operation is deletion. Once we have found the node to be deleted, we need to consider several possibilities. If the node is a leaf , it can be deleted immediately. http://ecomputernotes.com
  • 3. Deleting a node in BST If the node has one child, the node can be deleted after its parent adjusts a pointer to bypass the node and connect to inorder successor. 6 2 4 3 1 8 http://ecomputernotes.com
  • 4. Deleting a node in BST The inorder traversal order has to be maintained after the delete. 6 2 4 3 1 8 6 2 4 3 1 8  http://ecomputernotes.com
  • 5. Deleting a node in BST The inorder traversal order has to be maintained after the delete. 6 2 4 3 1 8 6 2 4 3 1 8 6 2 3 1 8   http://ecomputernotes.com
  • 6. Deleting a node in BST The complicated case is when the node to be deleted has both left and right subtrees. The strategy is to replace the data of this node with the smallest data of the right subtree and recursively delete that node. http://ecomputernotes.com
  • 7. Deleting a node in BST Delete(2): locate inorder successor 6 2 5 3 1 8 4 Inorder successor http://ecomputernotes.com
  • 8. Deleting a node in BST Delete(2): locate inorder successor 6 2 5 3 1 8 4 Inorder successor Inorder successor will be the left-most node in the right subtree of 2. The inorder successor will not have a left child because if it did, that child would be the left-most node. http://ecomputernotes.com
  • 9. Deleting a node in BST Delete(2): copy data from inorder successor 6 2 5 3 1 8 4  6 3 5 3 1 8 4 http://ecomputernotes.com
  • 10. Deleting a node in BST Delete(2): remove the inorder successor 6 2 5 3 1 8 4  6 3 5 3 1 8 4  6 3 5 3 1 8 4 http://ecomputernotes.com
  • 11. Deleting a node in BST Delete(2)  6 3 5 4 1 8  6 3 5 3 1 8 4 http://ecomputernotes.com
  • 12. C++ code for delete ‘ delete’ is C++ keyword. We will call our deleteNode routine remove. Here is the C++ code for remove. http://ecomputernotes.com
  • 13. C++ code for delete TreeNode<int>* remove(TreeNode<int>* tree, int info) { TreeNode<int>* t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }  http://ecomputernotes.com
  • 14. C++ code for delete TreeNode<int>* remove(TreeNode<int>* tree, int info) { TreeNode<int>* t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }  http://ecomputernotes.com
  • 15. C++ code for delete TreeNode<int>* remove(TreeNode<int>* tree, int info) { TreeNode<int>* t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }  http://ecomputernotes.com
  • 16. C++ code for delete TreeNode<int>* remove(TreeNode<int>* tree, int info) { TreeNode<int>* t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }  http://ecomputernotes.com
  • 17. C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }  http://ecomputernotes.com
  • 18. C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }  http://ecomputernotes.com
  • 19. C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }  http://ecomputernotes.com
  • 20. C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }  http://ecomputernotes.com
  • 21. C++ code for delete //two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }  http://ecomputernotes.com
  • 22. C++ code for delete else { // case 1 TreeNode<int>* nodeToDelete = tree; if( tree->getLeft() == NULL ) // will handle 0 children tree = tree->getRight(); else if( tree->getRight() == NULL ) tree = tree->getLeft(); else tree = NULL; delete nodeToDelete; } return tree; }  http://ecomputernotes.com
  • 23. C++ code for delete else { // case 1 TreeNode<int>* nodeToDelete = tree; if( tree->getLeft() == NULL ) // will handle 0 children tree = tree->getRight(); else if( tree->getRight() == NULL ) tree = tree->getLeft(); else tree = NULL; delete nodeToDelete; } return tree; }  http://ecomputernotes.com
  • 24. C++ code for delete else { // case 1 TreeNode<int>* nodeToDelete = tree; if( tree->getLeft() == NULL ) // will handle 0 children tree = tree->getRight(); else if( tree->getRight() == NULL ) tree = tree->getLeft(); else tree = NULL; delete nodeToDelete; } return tree; }  http://ecomputernotes.com
  • 25. C++ code for delete TreeNode<int>* findMin(TreeNode<int>* tree) { if( tree == NULL ) return NULL; if( tree->getLeft() == NULL ) return tree; // this is it. return findMin( tree->getLeft() ); }  http://ecomputernotes.com
  • 26. C++ code for delete TreeNode<int>* findMin(TreeNode<int>* tree) { if( tree == NULL ) return NULL; if( tree->getLeft() == NULL ) return tree; // this is it. return findMin( tree->getLeft() ); }  http://ecomputernotes.com
  • 27. C++ code for delete TreeNode<int>* findMin(TreeNode<int>* tree) { if( tree == NULL ) return NULL; if( tree->getLeft() == NULL ) return tree; // this is it. return findMin( tree->getLeft() ); }  http://ecomputernotes.com
  • 28. BinarySearchTree.h Let us design the BinarySearchTree class (factory). http://ecomputernotes.com
  • 29. BinarySearchTree.h #ifndef _BINARY_SEARCH_TREE_H_ #define _BINARY_SEARCH_TREE_H_ #include <iostream.h> // For NULL // Binary node and forward declaration template <class EType> class BinarySearchTree;  http://ecomputernotes.com
  • 30. BinarySearchTree.h #ifndef _BINARY_SEARCH_TREE_H_ #define _BINARY_SEARCH_TREE_H_ #include <iostream.h> // For NULL // Binary node and forward declaration template <class EType> class BinarySearchTree;  http://ecomputernotes.com
  • 31. BinarySearchTree.h template <class EType> class BinaryNode { EType element; BinaryNode *left; BinaryNode *right; BinaryNode( const EType & theElement, BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ), right( rt ) { } friend class BinarySearchTree<EType>; };  http://ecomputernotes.com
  • 32. BinarySearchTree.h template <class EType> class BinaryNode { EType element; BinaryNode *left; BinaryNode *right; BinaryNode( const EType & theElement, BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ), right( rt ) { } friend class BinarySearchTree<EType>; };  http://ecomputernotes.com
  • 33. BinarySearchTree.h template <class EType> class BinaryNode { EType element; BinaryNode *left; BinaryNode *right; BinaryNode( const EType & theElement, BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ), right( rt ) { } friend class BinarySearchTree<EType>; };  http://ecomputernotes.com
  • 34. BinarySearchTree.h template <class EType> class BinaryNode { EType element; BinaryNode *left; BinaryNode *right; BinaryNode( const EType & theElement, BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ), right( rt ) { } friend class BinarySearchTree<EType>; };  http://ecomputernotes.com
  • 35. BinarySearchTree.h template <class EType> class BinarySearchTree { public: BinarySearchTree( const EType& notFound ); BinarySearchTree( const BinarySearchTree& rhs ); ~BinarySearchTree( ); const EType& findMin( ) const; const EType& findMax( ) const; const EType& find( const EType & x ) const; bool isEmpty( ) const; void printInorder( ) const; http://ecomputernotes.com
  • 36. BinarySearchTree.h void insert( const EType& x ); void remove( const EType& x ); const BinarySearchTree & operator= ( const BinarySearchTree & rhs ); http://ecomputernotes.com
  • 37. BinarySearchTree.h private: BinaryNode<EType>* root; // ITEM_NOT_FOUND object used to signal failed finds const EType ITEM_NOT_FOUND; const EType& elementAt( BinaryNode<EType>* t ); void insert(const EType& x, BinaryNode<EType>* & t); void remove(const EType& x, BinaryNode<EType>* & t); BinaryNode<EType>* findMin(BinaryNode<EType>* t); BinaryNode<EType>* findMax(BinaryNode<EType>* t); BinaryNode<EType>* find(const EType& x, BinaryNode<EType>* t ); void makeEmpty(BinaryNode<EType>* & t); void printInorder(BinaryNode<EType>* t); }; #endif http://ecomputernotes.com

Editor's Notes

  • #8: Start lecture 16
  • #12: End of lecture 15
  • #38: End of lecture 16